#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.8.1
 Author:         myName

 Script Function:
	Template AutoIt script.

#ce ----------------------------------------------------------------------------

Func forecast_count_right(ByRef $map,$r,$c,$n)
	Local $v = 0
	Local $z = 0
	Local $row = ""
	Local $last = $c + $n - 1
	If $last >= $col_length Then
		SetExtended(0)
		Return $row
	EndIf
	For $i = $c To $last
		If $map[$r][$i] = 0 Then
			$row &= "*"
			$z += 1
		ElseIf $v = 0 Then
			$v = $map[$r][$i]
			$row &= $v
		ElseIf $v <> $map[$r][$i] Then
			SetExtended(0)
			Return $row
		Else
			$row &= $v
		EndIf
	Next
	SetExtended($z)
	Return $row
EndFunc

Func forecast_count_bottom(ByRef $map,$r,$c,$n)
	Local $v = 0
	Local $z = 0
	Local $row = ""
	Local $last = $r + $n - 1
	If $last >= $row_length Then
		SetExtended(0)
		Return $row
	EndIf
	For $i = $r To $last
		If $map[$i][$c] = 0 Then
			$row &= "*"
			$z += 1
		ElseIf $v = 0 Then
			$v = $map[$i][$c]
			$row &= $v
		ElseIf $v <> $map[$i][$c] Then
			SetExtended(0)
			Return $row
		Else
			$row &= $v
		EndIf
	Next
	SetExtended($z)
	Return $row
EndFunc

Global Const $chance = 1/(UBound($field,1)-1)
Global $chances[6] = [ _
	$chance, _
	$chance * $chance, _
	$chance * $chance * $chance, _
	$chance * $chance * $chance * $chance, _
	$chance * $chance * $chance * $chance * $chance, _
	$chance * $chance * $chance * $chance * $chance * $chance _
]

Func forecast_count_drop(ByRef $map, ByRef $award, ByRef $log)
	Local $res[4] = [0,0,0,0]
	If $log Then
		$log &= "forecast_count_drop" & @CRLF
	EndIf
	Local $row,$count
	For $r = 0 To $row_length - 1
		For $c = 0 To $col_length - 1
			For $l = 3 To 6
				$row = forecast_count_right($map,$r,$c,$l)
				$count = @extended
				If $count > 0 Then
					$res[$l - 3] += $chances[$count - 1]
					If $log Then $log &= "horizomtal [" & $r & "," & $c & "] l=" & $l & " cnt=" & $count & " '" & $row & "'" & @CRLF
				EndIf
				$row = forecast_count_bottom($map,$r,$c,$l)
				$count = @extended
				If $count > 0 Then
					$res[$l - 3] += $chances[$count - 1]
					If $log Then $log &= "vertical [" & $r & "," & $c & "] l=" & $l & " cnt=" & $count & " '" & $row & "'" & @CRLF
				EndIf
			Next
		Next
	Next
	$award[$l_award + 4] += $res[0]
	$award[$l_award + 5] += $res[1]
	$award[$l_award + 6] += $res[2]
	$award[$l_award + 7] += $res[3]
EndFunc

Func forecast_calculate(ByRef $imap,ByRef $aw, ByRef $log)
	;If $log Then $log &= "forecast_calculate before: " & award_to_str($aw) & @CRLF
	Local $variant = find_variant_light($imap)
	If IsArray($variant) Then
		;If $log Then $log &= "forecast_calculate has: " & UBound($variant,1) & @CRLF
		Local $m[$l_award + 2]
		For $i = 0 To $l_award + 1
			$m[$i] = 0
		Next
		For $v = 0 To UBound($variant,1) - 1
			;[$m,$g,$r,$c,$award]
			Local $var = $variant[$v]
			If $log Then $log &= "enemy forecast: " & award_to_str($var[4]) & @CRLF
			Local $award = $var[4]
			For $i = 0 To $l_award + 1
				If $m[$i] < $award[$i] Then
					$m[$i] = $award[$i]
				EndIf
			Next
		Next
		$aw[10] = $m
	EndIf
	;If $log Then $log &= "forecast_calculate after: " & award_to_str($aw) & @CRLF
EndFunc
